package com.belief.authlogin.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

/**
 * 版权：(C) 版权所有 苏州彼立孚数据科技有限公司
 * <简述>
 * <详细描述>
 *
 * @author Lynkin
 * @create: 2021-07-15 10:52
 */
public class RegeditUtils {

    // 把相应的值储存到变量中去
    public static void writeValue(String key, String value) {
        // HKEY_LOCAL_MACHINE\Software\JavaSoft\prefs下写入注册表值.
        Preferences pre = Preferences.systemRoot().node("/javaplayer");
        pre.put(key, value);
//        for (int i = 0; i < keys.length; i++) {
//            pre.put(keys[i], values[i]);
//        }
    }



    /***
     * 根据key获取value
     *

     */


    public static String getValue(String key) {
        Preferences pre = Preferences.systemRoot().node("/javaplayer");
        return pre.get(key, "time");
    }

    /***
     * 清除注册表
     *
     * @throws BackingStoreException

     */


    public void clearValue() throws BackingStoreException {
        Preferences pre = Preferences.systemRoot().node("/javaplayer");
        pre.clear();
    }


    public static void main(String[] args) {
        RegeditUtils reg = new RegeditUtils();
        reg.writeValue("test", "asydgyuasgdyuas");
        System.out.println(reg.getValue("test"));
        //可以读取任意路径下的、
        try {
            Process ps = null;
            ps = Runtime.getRuntime().exec("reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Prefs\\javaplayer");
            ps.getOutputStream().close();
            InputStreamReader i = new InputStreamReader(ps.getInputStream());
            String line;
            BufferedReader ir = new BufferedReader(i);
            while ((line = ir.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
